/*
 * TermSequentialFile.cpp
 *
 *  Created on: Nov 26, 2012
 *      Author: m
 */

#include "TermSequentialFile.h"

namespace physical
{

TermSequentialFile::TermSequentialFile() :
        m_file( new FixedSizeBlockSequentialFile<TERM_SIZE_IN_BYTES> ),
        m_serializer( new TermSerializer )
{
}

TermSequentialFile::TermSequentialFile( const char* filename,
        openmode mode ) :
        m_file( new FixedSizeBlockSequentialFile<TERM_SIZE_IN_BYTES>( filename, mode ) ),
        m_serializer( new TermSerializer )
{
}

TermSequentialFile::~TermSequentialFile()
{
    m_file->close();
}

Term TermSequentialFile::get()
{
    byte buffer[TERM_SIZE_IN_BYTES];
    Term term;

    m_file->get( buffer );
    m_serializer->deserialize( buffer, term );

    return term;
}

size_t TermSequentialFile::put( const Term& term )
{
    byte buffer[TERM_SIZE_IN_BYTES];

    if ( term.getTerm().length() >= TERM_SIZE_IN_BYTES )
    {
        throw TermIsTooLongException();
    }

    m_serializer->serialize( term, buffer );

    return m_file->put( buffer ) + 1;
}

bool TermSequentialFile::hasNext()
{
    return m_file->hasNext();
}

void TermSequentialFile::open( const char* filename, openmode mode )
{
    m_file->open( filename, mode );
}

void TermSequentialFile::close()
{
    m_file->close();
}

} /* namespace physical */
